
package com.angel.sensors.model.types;


import com.angel.sensors.R;
import com.angel.sensors.model.BaseMeter;
import com.angel.sensors.model.DerivedMeter;
import com.angel.sensors.model.MeterType;
import com.angel.sensors.model.NumericComponent;
import com.angel.sensors.model.SensorComponent;
import com.angel.sensors.model.SensorStrings;
import com.angel.sensors.services.SensorService;

import android.content.Context;

public class PresenceMeter extends DerivedMeter implements BaseMeter.ValueListener {   
    private static final long TIMER_INTERVAL        = 1000L;
    private static final long INACTIVITY_THRESHOLD  = 5000L;

    private AccelerationMeter accelerationMeter;
    
    private double x_accel = 0;
    private double y_accel = 0;
    private double z_accel = 0;
    private static final double ACCELERATION_THRESHOLD = 0.025;

    private long lastPresence;
        
    public PresenceMeter(Context context) {
        super(context);
        
        accelerationMeter = (AccelerationMeter) SensorService.getInstance().getMeter(MeterType.ACCELERATION);
        //SensorService.getInstance().getMeter(SensorService.SOUND_LEVEL_SENSOR);
        
        initComponents(new SensorComponent[]{
            new NumericComponent(SensorStrings.VALUE, context.getResources().getString(R.string.value), 0, 1, 0, null)
        });
    }

    public boolean isPresent() {
        return accelerationMeter.isPresent();
    }

    @Override
    public int getType() {
        return MeterType.PRESENCE;
    }
    
    public String getName() {
        return "presence";
    }
    
    public String getText() {
        return context.getResources().getString(R.string.presence);
    }

    @Override
    protected void onStart() {
        updateNumericValue(SensorStrings.VALUE, 0);
        accelerationMeter.registerListener(this);
        startTimer(TIMER_INTERVAL);
    }

    @Override
    protected void onStop() {
        accelerationMeter.unregisterListener(this);
        stopTimer();
    }
    
    @Override 
    protected void onTimer() {
        long now = System.currentTimeMillis();
        NumericComponent c = (NumericComponent)getComponent(SensorStrings.VALUE);
        
        if (now - lastPresence > INACTIVITY_THRESHOLD) {
            if (!c.isDefined() || c.getCurrent() != 0) {
                updateNumericValue(SensorStrings.VALUE, 0);
            }
        }
    }
    
    
    public void onValueChange(BaseMeter meter) {
        double x = ((NumericComponent)meter.getComponent(SensorStrings.X_COMPONENT)).getCurrent();
        double y = ((NumericComponent)meter.getComponent(SensorStrings.Y_COMPONENT)).getCurrent();
        double z = ((NumericComponent)meter.getComponent(SensorStrings.Z_COMPONENT)).getCurrent();
        
        if (Math.abs(x - x_accel) > ACCELERATION_THRESHOLD ||
            Math.abs(y - y_accel) > ACCELERATION_THRESHOLD ||
            Math.abs(z - z_accel) > ACCELERATION_THRESHOLD) {

            lastPresence = System.currentTimeMillis();
            updateNumericValue(SensorStrings.VALUE, 1);
            
            x_accel = x;
            y_accel = y;
            z_accel = z;    
        }
    }
}